package com.covery;

import com.covery.dto.SameFile;
import lombok.val;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class ChangeTest {

    public static void main(String[] args) {
        File origin = new File("C:\\Users\\Administrator\\Documents\\My Games\\They Are Billions\\Saves");
        String  path = "E:\\save";
        File save = new File(path);
        val list = origin.listFiles();
        if (list != null) {
            for (File file: list) {
                File file1 = new File(path+"\\" + file.getName());
                if (file1.exists()) {
                    val f = isSameFiles(file,file1);
                    System.out.println("文件名称："+file.getName()+"leftMd5:" + f.getMd5Left()+",right:" + f.getMd5Right() + ",equal:" + f.isSame());
                }
            }
        }

    }

    private static SameFile isSameFiles(File fileByte1, File fileByte2)  {
        try (InputStream inputStream = new FileInputStream(fileByte1);
             InputStream right = new FileInputStream(fileByte2)){
            String firstFileMd5 = DigestUtils.md5Hex(inputStream);

            String  secondFileMd5 = DigestUtils.md5Hex(right);
            val same = new SameFile(firstFileMd5,secondFileMd5);
            if (firstFileMd5.equals(secondFileMd5)) {
                System.out.println("---- equals ------ md5 " + firstFileMd5);
                same.setSame(true);
            } else {
                System.out.println(firstFileMd5 + " is firstFileMd5 ++ unequal ++ secondFileMd5 = " + secondFileMd5);
                same.setSame(false);
            }
            return same;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new SameFile();
    }
}
